package m.html.form;

import m.html.element.Element;
import m.html.element.attr.Align;
import m.html.util.VueListUtil;

import java.util.ArrayList;

public class TableColumn implements ITableColumn{
    private final String title;
    private final String field;
    private Element show;
    private Align align;
    private Integer width;
    private Align fixed;
    private boolean indexColumn;

    private String dict;
    private String dateFormat;//yyyy-MM-dd HH:mm:ss:SSS
    private String numberFormat;//#,##0.00
    private boolean numberSum=false;

    private TableEdit edit;
    private FormEvent link;
    public static TableColumn INDEX_COLUMN=index("序号");
    public static TableColumn index(String title){
        TableColumn col=text(title,"temp_index",50);
        col.indexColumn=true;
        col.fixed=Align.LEFT;
        col.align=Align.RIGHT;
        return col;
    }
    /**
     * 文本类型
     */
    public static TableColumn text(String title,String field){
        return text(title,field,120);
    }
    /**
     * 文本类型
     */
    public static TableColumn text(String title,String field,int width){
        return new TableColumn(title,field,Align.LEFT,width);
    }
    /**
     * 文本类型
     */
    public static TableColumn text(String title,String field,int width,String dict){
        return new TableColumn(title,field,Align.LEFT,width).setDict(dict);
    }
    /**
     * 数字类型 默认右对齐 #,##0.00
     */
    public static TableColumn number(String title,String field){
        return number(title,field,120);
    }
    /**
     * 数字类型 默认右对齐 #,##0.00
     */
    public static TableColumn number(String title,String field,int width){
        return number(title,field,width,"#,##0.00");
    }
    /**
     * 数字类型 默认右对齐 #,##0.00
     */
    public static TableColumn number(String title,String field,int width,String numberFormat){
        return new TableColumn(title,field,Align.RIGHT,width).setNumberFormat(numberFormat).isSum(true);
    }
    /**
     * 居中类型
     */
    public static TableColumn center(String title,String field){
        return center(title,field,120);
    }
    /**
     * 居中类型
     */
    public static TableColumn center(String title,String field,int width){
        return new TableColumn(title,field,Align.CENTER,width);
    }
    /**
     * 居中类型
     */
    public static TableColumn center(String title,String field,int width,String dict){
        return new TableColumn(title,field,Align.CENTER,width).setDict(dict);
    }
    protected TableColumn(String title,String field,Align align,Integer width){
        this.title=title;
        this.field=field;
        this.align=align;
        this.width=width;
        this.indexColumn=false;
    }
    public String getCustomRender(){
        Element temp1=Element.tag("template").A("#"+this.field.replaceAll("\\.", "_"),"{text, record, index}");
        Element tmp=Element.tag("div").A(":style","{textDecoration:record.deleteFlag==1?'line-through':'none'}");
        if(this.indexColumn) {
            tmp.E("{{index+1}}");
        }else if(null!=this.show){
            tmp.E(this.show);
        }else {
            if(null!=link) {
                Element a=Element.tag("a-typography-link").E(VueListUtil.toText(this.dateFormat,this.numberFormat,this.dict,new ArrayList<>(),false));
                a.A("@click",link.methodName);
                tmp.E(a);
            }else{//文本数字日期字典类型
                tmp.E(VueListUtil.toText(this.dateFormat, this.numberFormat, this.dict, new ArrayList<>(), false));
            }
        }
        if(null!=edit){
            temp1.E(edit.getItemElement("editRecord",this.field).A("v-if","index==editRowIndex"));
            tmp.A("v-else");
        }
        temp1.E(tmp);
        return temp1.toString();
    }

    /**
     * 设置时间格式
     */
    public TableColumn setDateFormat(String dateFormat){
        this.dateFormat=dateFormat;
        return this;
    }

    /**
     * 设置数字格式
     */
    public TableColumn setNumberFormat(String numberFormat){
        this.numberFormat=numberFormat;
        return this;
    }

    /**
     * 设置编辑组件
     */
    public TableColumn setEdit(TableEdit input){
        this.edit=input;
        return this;
    }

    /**
     * 编辑时 显示元素
     */
    public TableColumn setEdit(Element showElement){
        this.edit=TableEdit.show(showElement);
        return this;
    }

    /**
     * 设置字典类型
     */
    public TableColumn setDict(String dict){
        this.dict=dict;
        return this;
    }

    /**
     * 设置冻结方向
     */
    public TableColumn setFixed(Align fixed) {
        this.fixed = fixed;
        return this;
    }

    /**
     * 设置显示内容 代替原有显示方式
     */
    public TableColumn setShow(Element show) {
        this.show = show;
        return this;
    }

    /**
     * 设置单元格链接
     */
    public TableColumn setLink(FormEvent link) {
        this.link = link;
        return this;
    }

    /**
     * 设置对齐方式
     */
    public TableColumn setAlign(Align align) {
        this.align = align;
        return this;
    }
    /**
     * 设置列宽度
     */
    public TableColumn setWidth(Integer width) {
        this.width = width;
        return this;
    }

    /**
     * 设置是否合计 数字有效
     */
    public TableColumn isSum(boolean sum){
        this.numberSum=sum;
        return this;
    }
    public boolean getSum(){
        return this.numberSum;
    }

    public FormEvent getLink() {
        return link;
    }


    public Element getShow() {
        return show;
    }

    public Align getFixed() {
        return fixed;
    }

    public String getDict() {
        return dict;
    }

    public TableEdit getEdit() {
        return edit;
    }

    public String getTitle() {
        return title;
    }

    public String getField() {
        return field;
    }

    public Align getAlign() {
        return align;
    }

    public Integer getWidth() {
        return width;
    }

    public String getDateFormat() {
        return dateFormat;
    }

    public String getNumberFormat() {
        return numberFormat;
    }

    @Override
    public FormEvent[] getLinks() {
        if(null!=link) return new FormEvent[]{link};
        return new FormEvent[0];
    }
}
